//
//  Help.m
//  001LiycSinaWeiBo
//
//  Created by 李伟国 on 15/9/8.
//  Copyright (c) 2015年 李伟国. All rights reserved.
//

#import "Help.h"

@implementation Help

+ (NSURL *)getAuthorizeURL {

//    一个字典 key value
//     基本URL
    
    NSDictionary *params = @{@"client_id":SINA_APP_KEY,@"redirect_uri":redirect_uri,@"display":@"mobile",@"response_type":@"token"};
    
    NSURL *authorizeURL = [Help connectWithAuthorizeURL:SINA_AUTHORIZE_SEVER andParams:params];

    return authorizeURL;
}

+ (NSURL *)connectWithAuthorizeURL:(NSString *)baseUrl andParams:(NSDictionary *)params {

//    https://api.weibo.com/oauth2/authorize?client_id=3601604349&display=mobile&redirect_uri=http://hi.baidu.com/jt_one&response_type=token;
    
//    client_id=3601604349&display=mobile&redirect_uri=http://hi.baidu.com/jt_one&response_type=token
    
    NSString *path;
    
    if (params == nil) {
        return [NSURL URLWithString:baseUrl];
    }else {
    
        NSMutableArray *keyValues = [NSMutableArray array];
        for (NSString *key in params.keyEnumerator) {
            NSLog(@"%@",key);
            NSString *keyAndValue = [NSString stringWithFormat:@"%@=%@",key,params[key]];
            NSLog(@"%@",keyAndValue);
            [keyValues addObject:keyAndValue];
            
        }
        NSLog(@"%@",keyValues);
    
        
        path = [keyValues componentsJoinedByString:@"&"];
        NSLog(@"%@",path);
        
        path = [NSString stringWithFormat:@"%@?%@",baseUrl,path];
        NSLog(@"%@",path);
        
    }
    
//    https://api.weibo.com/oauth2/authorize?client_id=3601604349&redirect_uri=http://hi.baidu.com/jt_one&display=mobile&response_type=token
    return [NSURL URLWithString:path];
}



+ (NSString *)getContentFor:(NSString *)need andPath:(NSString *)path {

//    access_token=
//    http://hi.baidu.com/jt_one#access_token=2.008kmTQDjoxjvDbce659177bPQkUGD&remind_in=123438&expires_in=123438&uid=2991918775
//    2.008kmTQDjoxjvDbce659177bPQkUGD
    
    if ([path containsString:need]) {
        NSRange needRange = [path rangeOfString:need];
        
        NSString *subStr = [path substringFromIndex:needRange.location];
        
        needRange = [subStr rangeOfString:need];
        
        NSRange yuRange = [subStr rangeOfString:@"&"];
        
        if (yuRange.location == NSNotFound) {
            return [subStr substringFromIndex:needRange.location + needRange.length];
        }
        
        
        NSRange newRange = NSMakeRange(needRange.location + needRange.length, yuRange.location - (needRange.location + needRange.length));
        
        return [subStr substringWithRange:newRange];
        
    } else {
    
        return nil;
    }
    
    return nil;
}


+ (BOOL)isNeedToRefreshTheToken {

    NSDate *expireDate = [[NSUserDefaults standardUserDefaults] objectForKey:EXPIRES_DATE];
    
    
    NSComparisonResult result = [expireDate compare:[NSDate date]];
    
    
    if (result == NSOrderedDescending) {
        
        return NO;//没有过期
    }
    
    return YES;//过期
    

}

@end










